package help;

import java.util.ArrayList;
import java.util.StringTokenizer;

import console.IOBuffer;

/**
 *
 * @author Michael Hanns
 *
 */
public abstract class Help {

    protected IOBuffer term;
    protected String helpLocation;
    protected boolean helpRead;
    protected ArrayList<HelpEntry> entries;
    protected boolean readingText;
    protected String textField;

    public abstract String query(String query);

    protected abstract void readHelpFiles();

    protected HelpEntry readLine(StringTokenizer line, HelpEntry entry) {
        String field = line.nextToken();

        if (field.startsWith("//")) {
            return entry;
        } else if (field.equalsIgnoreCase("@title")) {
            if (entry != null) {
                if (readingText) {
                    entry.setText(textField);
                    readingText = false;
                }
                entries.add(entry);
            }
            textField = "";
            
            // Empty out the first demlimiter
            line.nextToken();
            entry = new HelpEntry(consolidateTokens(line));
        } else if (field.equalsIgnoreCase("@text")) {
            readingText = true;
            // Empty out the first demlimiter
            line.nextToken();
            
            textField = consolidateTokens(line);
        } else if (field.equalsIgnoreCase("@keywords")) {
            if (readingText) {
                entry.setText(textField);
                readingText = false;
            }
            
            ArrayList<String> keysAR = new ArrayList<String>();
            String[] keywords = new String[line.countTokens()];
            for (int x = 0; x < keywords.length; x++) {
                keywords[x] = line.nextToken();
                if(keywords[x].trim().length() > 0) {
                    keysAR.add(keywords[x]);
                }
            }
            keywords = new String[keysAR.size()];
            keywords = keysAR.toArray(keywords);
            
            
            entry.setKeywords(keywords);
        } else if (field.equalsIgnoreCase("@related")) {
            if (readingText) {
                entry.setText(textField);
                readingText = false;
            }
            ArrayList<String> relAR = new ArrayList<String>();
            String[] related = new String[line.countTokens()];
            for (int x = 0; x < related.length; x++) {
                related[x] = line.nextToken();
                if(related[x].trim().length() > 0) {
                    relAR.add(related[x]);
                }
            }
            related = new String[relAR.size()];
            related = relAR.toArray(related);
            
            
            entry.setRelated(related);
        } else if (readingText) {
            textField += "\n" + field + consolidateTokens(line);
            entry.setText(textField);
        }
        return entry;
    }

    protected HelpEntry readLine(HelpEntry entry) {
        if (readingText) {
            textField += "\n ";
            entry.setText(textField);
        }
        return entry;
    }

    protected String consolidateTokens(StringTokenizer tkns) {
        String line = "";
        if (tkns.hasMoreTokens()) {
            line = tkns.nextToken();
        }
        while (tkns.hasMoreTokens()) {
            line += tkns.nextToken();
        }
        return line;
    }

}
